/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.store.client.components;

import java.util.ArrayList;
import java.util.List;

import com.elf.gwt.common.client.model.AbstractModel;
import com.elf.gwt.common.client.model.LanguageModel;
import com.elf.gwt.common.client.utils.CommonUtils;
import com.elf.gwt.store.client.StoreComponentController;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTMLTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;

/**
 *  
 */
public class LanguagesComponent extends JSONServerComponent
{	
	protected Grid bodyPanel;
	protected LanguageModel languageModel;
	protected List<LanguageModel> languageModels;
	protected int maxNumCols = 5;
	protected int numCols = 0;
	protected String defaultCode = "";
	protected String currentCode = "";
		
	/**
	 * Constructor
	 */
	public LanguagesComponent(StoreComponentController controller, String stylePrefix)
	{
		super(controller, stylePrefix, null, false);

		languageModel = new LanguageModel(1, "English", "en", "", true);
		languageModels = new ArrayList<LanguageModel>();
		
		panel.getCellFormatter().setHorizontalAlignment(1, 0, HasHorizontalAlignment.ALIGN_CENTER);
	}
	public void onBuilt()
	{
		if (languageModels.size() > 1) {
			super.onBuilt();
		}
		else {
			setVisible(false);
		}
	}
	
	protected void buildMetaModel(JSONObject jsonObject) throws Exception
	{
		defaultCode = AbstractModel.convertToString(jsonObject, "default_code");
		currentCode = AbstractModel.convertToString(jsonObject, "current_code");
	}
	
	protected void buildModel(JSONArray jsonArray) throws Exception
	{
		languageModels.clear();
		super.buildModel(jsonArray);
	}
	
	protected void buildModel(JSONObject jsonObject) throws Exception
	{
		LanguageModel model = new LanguageModel(jsonObject, defaultCode);
		languageModels.add(model);
		if (model.getCode().equals(currentCode))
		{
			languageModel.update(model);
		}
	}

	public void buildFromModel() throws Exception
	{
		int sz = languageModels.size();
		numCols = Math.min(maxNumCols, sz);
		int numRows = (int)Math.round(Math.ceil((1.0 * sz)/(numCols * 1.0)));
		bodyPanel = new Grid(numRows, numCols);
		bodyPanel.setStyleName("ComponentBodyPanel");
		bodyPanel.addStyleName(getHandle() + "BodyPanel");
		bodyPanel.addClickHandler(tableHandler); 
		CellFormatter cf = bodyPanel.getCellFormatter();
		for (int i = 0; i < sz; i++)
		{
			LanguageModel model = languageModels.get(i);
			
			int row = (int)Math.round(Math.floor((1.0 * i)/(numCols * 1.0)));
			int col = i - (row * numCols);
						
			Image image = new Image();
			image.setUrl(getImageDirectory(model) + model.getImage());
				
			bodyPanel.setWidget(row, col, image);
			cf.setStyleName(row, col, "ComponentLink");
			cf.addStyleName(row, col, getHandle() + "Item");
			cf.setHorizontalAlignment(row, col, HasHorizontalAlignment.ALIGN_CENTER);
		}

		panel.setWidget(1, 0, bodyPanel);
		onBuilt();
	}

	protected ClickHandler tableHandler = new ClickHandler() {
		public void onClick(ClickEvent event)
		{
			HTMLTable.Cell cell = bodyPanel.getCellForEvent(event);
			int rowIndex = cell.getRowIndex();
			int cellIndex = cell.getCellIndex();
			LanguageModel model  = getLanguageModel((rowIndex * numCols) + cellIndex);
			onSelection("LanguagesComponent--" + model.getCode());
		}
	};

	public LanguageModel getLanguageModel(int index) {
		return languageModels.get(index);
	}

	public LanguageModel getLanguageModel() {
		return languageModel;
	}

	public String getImageDirectory(LanguageModel model)
	{
		return CommonUtils.getImageDirectory() + getHandle() + "/" + model.getCode() + "/";
	}
}
